package yyy.ab.common.cfgBean;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;
import yyy.ab.common.utils.SecurityUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * ab权限验证配置
 */
@Service(value = "ab")
public class AbPermissionConfig {

    public Boolean check(String... permissions) {
        // 获取当前用户的所有权限
        List<String> abPermissions = SecurityUtils.getCurrentUser().getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList());
        // 判断当前用户的所有权限是否包含接口上定义的权限
        return abPermissions.contains("admin") || Arrays.stream(permissions).anyMatch(abPermissions::contains);
    }
}
